﻿using System.Linq;
using Lucid.ESA.ClientDataSystem.Domain.Common;
using Lucid.ESA.ClientDataSystem.Domain.ParameterClasses;

namespace Lucid.ESA.ClientDataSystem.Domain.Entities
{
    public class Site : Entity
    {
        protected string _addressLine1;
        protected string _addressLine2;
        protected string _addressLine3;
        protected string _addressLine4;
        protected string _postCode;
        protected bool _mainSite;
        protected bool _deleted;
        protected Client _client;

        public virtual string AddressLine1
        {
            get { return _addressLine1; }
            set { _addressLine1 = value; }
        }

        public virtual string AddressLine2
        {
            get { return _addressLine2; }
            set { _addressLine2 = value; }
        }

        public virtual string AddressLine3
        {
            get { return _addressLine3; }
            set { _addressLine3 = value; }
        }

        public virtual string AddressLine4
        {
            get { return _addressLine4; }
            set { _addressLine4 = value; }
        }

        public virtual string PostCode
        {
            get { return _postCode; }
            set { _postCode = value; }
        }

        public virtual bool MainSite
        {
            get { return _mainSite; }
            set { _mainSite = value; }
        }

        public virtual bool Deleted
        {
            get { return _deleted; }
            set { _deleted = value; }
        }

        public virtual Client Client
        {
            get { return _client; }
            set { _client = value; }
        }

        public static Site Create(CreateSiteParameters parameters)
        {
            Site site = new Site();
            site.AddressLine1 = parameters.AddressLine1;
            site.AddressLine2 = parameters.AddressLine2;
            site.AddressLine3 = parameters.AddressLine3;
            site.AddressLine4 = parameters.AddressLine4;
            site.PostCode = parameters.PostCode;
            site.MainSite = parameters.MainSite;
            site.Deleted = false;
            site.Client = parameters.Client;

            if(site.Client != null)
                site.Client.Sites.Add(site);

            return site;
        }

        public virtual void Edit(EditSiteParameters parameters)
        {
            _addressLine1 = parameters.AddressLine1;
            _addressLine2 = parameters.AddressLine2;
            _addressLine3 = parameters.AddressLine3;
            _addressLine4 = parameters.AddressLine4;
            _postCode = parameters.PostCode;
            _mainSite = parameters.MainSite;
        }

        public virtual void Delete()
        {
            _deleted = true;
        }

        public virtual ValidationMessageCollection Validate()
        {
            ValidationMessageCollection messages = new ValidationMessageCollection();

            //AddressLine 1 is required.
            if (_addressLine1 == null || _addressLine1 == string.Empty)
                messages.AddError("AddressLine1", "Address line 1 cannot be empty.");

            //Client is required.
            if (_client == null)
                messages.AddError("Client", "Client is not set.");

            //Ensure there is only one main site.
            if (_client != null)
            {
                int mainSiteCount = (
                    from site in _client.Sites
                    where site.MainSite
                    && !site.Deleted
                    select site)
                    .Count();

                if (mainSiteCount > 1)
                    messages.AddError("MainSite", "Site cannot be set as main site because a main site already exists for this client.");
            }
            
            return messages;
        }
    }
}
